#  this file hold some data model used by hkday
import datetime
from google.appengine.ext import db

'''
    Search Result Data
'''
class SearchResult(db.Model):
    # query keyword
    query = db.StringProperty(required=True)
    # search result item id
    id = db.StringProperty(required=True)
    # search result item title
    title = db.StringProperty(required=True)
    # search result item summary
    summary = db.TextProperty(required=True)
    # search result item url
    url  = db.StringProperty(required=True)
    # search result item clickurl
    clickurl = db.StringProperty(required=True)
    # search result page 's images
    images = db.StringListProperty()
    # search result clustered into group
    group = db.IntegerProperty(required=True)
    # search result group description
    groupdescription = db.StringProperty(required=True)
    # search result item 's displayed image
    displayimageurl = db.StringProperty(required=True)
    # control set of the getting displayed image
    displayimageset = db.BooleanProperty(required=True)
    # current displayed image 's width
    displayimagewidth = db.IntegerProperty()
    # current displayed image 's height  
    displayimageheight = db.IntegerProperty()
    # current displayed image 's index in this page's images
    displayimageindex = db.IntegerProperty(required=True)
    # page rank in search engine
    #pagerank = db.IntegerProperty(required=True)
    # loading image weight
    #weight = db.IntegerProperty(required=True)

'''
    User Log Data
'''
class UserLog(db.Model):
    useraddr = db.StringProperty(required=True)
    searchhit = db.IntegerProperty(required=True)
    lastvisit = db.DateTimeProperty(required=True)
    
    def record(self):
        userlogq = db.GqlQuery("SELECT * FROM UserLog WHERE useraddr = :1",self.useraddr)
        user_existed = userlogq.count(1)
        if user_existed == 0:
            self.put()
        else:
            for eachlog in userlogq.fetch(1):
                eachlog.searchhit += 1
                eachlog.lastvisit = datetime.datetime.now()
                eachlog.put()
                return
        print "put_user_log"
